package ai.people.core.plugin.config;

import ai.people.core.plugin.command.JarListenerRunner;
import ai.people.core.plugin.listener.JarListenerFactory;
import ai.people.core.plugin.properties.PluginProperties;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

/**
 * 文件配置
 *
 * @author yuanqinglong
 * @date 2022/6/13 20:34
 */
@Configuration
@RequiredArgsConstructor
@Import(value = {PluginProperties.class})
@ConditionalOnProperty(value = "plugin.listener.dir")
public class JarConfiguration {

    private final PluginProperties pluginProperties;

    /**
     * 文件侦听器工厂
     *
     * @param configurableApplicationContext 可配置应用程序上下文
     * @return {@link JarListenerFactory}
     */
    @Bean
    public JarListenerFactory fileListenerFactory(ConfigurableApplicationContext configurableApplicationContext) {
        return new JarListenerFactory(configurableApplicationContext, pluginProperties);
    }


    /**
     * jar侦听器
     *
     * @param fileListenerFactory 文件侦听器工厂
     * @return {@link JarListenerRunner}
     */
    @Bean
    public JarListenerRunner jarListenerRunner(JarListenerFactory fileListenerFactory) {
        return new JarListenerRunner(fileListenerFactory);
    }

}
